﻿using System;
using System.Collections.Generic;
using System.Linq;

using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.OrganizationalLife
{
    public partial class MyMemberCostList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                //return "MyMemberCostListView";
                return "";
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true,true);
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {

            var year = DateTime.Now.Year;
            for (int i = year - 5; i <= year + 5; i++)
            {
                this.ddlYear.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
            }
            this.ddlYear.SelectedValue = year.ToString();
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

        }
        public void LoadData()
        {
            var q = from item in DB.PartyMemberCosts where
                    item.LoginID==CurrentUser.LoginID && item.Year == this.ddlYear.SelectedValue
                    select new {Month=item.Month+"月",item.Cost,item.PayDate };

            // 在查询添加之后，排序和分页之前获取总记录数
            Grid1.RecordCount = q.Count();

            // 排列和分页
            q = SortAndPage(q, Grid1);

            Grid1.DataSource = q;
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string MyMemberCostListId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("MyMemberCostListDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var userCount = DB.PartyArticles.Count(t => t.ID == MyMemberCostListId);
                if (userCount > 0)
                {
                    Alert.ShowInTop("删除失败！需要先清空作用此专题的题目！");
                    return;
                }

                var del = DB.PartyArticles.FirstOrDefault(t => t.ID == MyMemberCostListId);
                DB.PartyArticles.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }
        
        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("MyMemberCostListDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyArticles.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyArticles.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }
}